package com.cloud.mall.user.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.ums.mapper.UmsRoleMapper;
import com.cloud.mall.ums.mapper.UmsRoleMenuRelationMapper;
import com.cloud.mall.ums.mapper.UmsRolePermissionRelationMapper;
import com.cloud.mall.ums.mapper.UmsRoleResourceRelationMapper;
import com.cloud.mall.ums.model.*;
import com.cloud.mall.user.dao.UmsRoleDao;
import com.cloud.mall.user.dao.UmsRolePermissionRelationDao;
import com.cloud.mall.user.input.UmsRoleInpDTO;
import com.cloud.mall.user.output.UmsMenuOut;
import com.cloud.mall.user.output.UmsPermissionOut;
import com.cloud.mall.user.output.UmsResourceOut;
import com.cloud.mall.user.output.UmsRoleOut;
import com.cloud.mall.usr.service.UmsRoleService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class UmsRoleServiceImpl implements UmsRoleService {
    @Autowired
    private UmsRoleMapper roleMapper;
    @Autowired
    private UmsRolePermissionRelationDao rolePermissionRelationDao;
    @Autowired
    private UmsRolePermissionRelationMapper rolePermissionRelationMapper;
    @Autowired
    private UmsRoleDao roleDao;
    @Autowired
    private UmsRoleMenuRelationMapper roleMenuRelationMapper;
    @Autowired
    private UmsRoleResourceRelationMapper roleResourceRelationMapper;


    @Override
    public BaseResponse create(@RequestBody UmsRoleInpDTO umsRoleInpDTO) {
        UmsRole role = MallBeanUtils.dtoToDo(umsRoleInpDTO, UmsRole.class);
        role.setCreateTime(new Date());
        role.setAdminCount(0);
        role.setSort(0);
        int count=roleMapper.insert(role);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse update(Long id,  @RequestBody UmsRoleInpDTO umsRoleInpDTO) {
        UmsRole role = MallBeanUtils.dtoToDo(umsRoleInpDTO, UmsRole.class);
        role.setId(id);
        int count=roleMapper.updateByPrimaryKeySelective(role);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse delete( List<Long> ids) {
        UmsRoleExample example = new UmsRoleExample();
        example.createCriteria().andIdIn(ids);
        int count= roleMapper.deleteByExample(example);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse<List<UmsPermissionOut>> getPermissionList(Long roleId) {
        List<UmsPermission>  umsPermissionList= rolePermissionRelationDao.getPermissionList(roleId);
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(umsPermissionList,UmsPermissionOut.class));
    }

    @Transactional
    @Override
    public BaseResponse updatePermission(Long roleId, List<Long> permissionIds) {
        //先删除原有关系
        UmsRolePermissionRelationExample example=new UmsRolePermissionRelationExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        rolePermissionRelationMapper.deleteByExample(example);
        //批量插入新关系
        List<UmsRolePermissionRelation> relationList = new ArrayList<>();
        for (Long permissionId : permissionIds) {
            UmsRolePermissionRelation relation = new UmsRolePermissionRelation();
            relation.setRoleId(roleId);
            relation.setPermissionId(permissionId);
            relationList.add(relation);
        }
        int count= rolePermissionRelationDao.insertList(relationList);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse<List<UmsRoleOut>> listAll() {
        List<UmsRole> umsRoleList = roleMapper.selectByExample(new UmsRoleExample());
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(umsRoleList,UmsRoleOut.class));
    }

    @Override
    public BaseResponse<CommonPage<UmsRoleOut>> list(String keyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        UmsRoleExample example = new UmsRoleExample();
        if (!StringUtils.isEmpty(keyword)) {
            example.createCriteria().andNameLike("%" + keyword + "%");
        }
        List<UmsRole> umsRoleList = roleMapper.selectByExample(new UmsRoleExample());
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(umsRoleList,UmsRoleOut.class)));
    }

    @Override
    public BaseResponse updateStatus(Long id, Integer status) {
        UmsRole umsRole = new UmsRole();
        umsRole.setStatus(status);
        umsRole.setId(id);
        int count=roleMapper.updateByPrimaryKeySelective(umsRole);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse<List<UmsMenuOut>> listMenu(Long roleId) {
        List<UmsMenu> menuList=roleDao.getMenuListByRoleId(roleId);
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(menuList,UmsMenuOut.class));
    }

    @Override
    public BaseResponse<List<UmsResourceOut>> listResource(Long roleId) {
        List<UmsResource>  resourceList= roleDao.getResourceListByRoleId(roleId);
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(resourceList,UmsResourceOut.class));
    }

    @Transactional
    @Override
    public BaseResponse allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        UmsRoleMenuRelationExample example=new UmsRoleMenuRelationExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleMenuRelationMapper.deleteByExample(example);
        //批量插入新关系
        for (Long menuId : menuIds) {
            UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            roleMenuRelationMapper.insert(relation);
        }
        int count=menuIds.size();
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Transactional
    @Override
    public BaseResponse allocResource(Long roleId, List<Long> resourceIds) {
        //先删除原有关系
        UmsRoleResourceRelationExample example=new UmsRoleResourceRelationExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleResourceRelationMapper.deleteByExample(example);
        //批量插入新关系
        for (Long resourceId : resourceIds) {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            roleResourceRelationMapper.insert(relation);
        }
        int count=resourceIds.size();
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }
}
